package cn.wolfcode.wms.service.impl;

import cn.wolfcode.wms.domain.Brand;
import cn.wolfcode.wms.domain.Product;
import cn.wolfcode.wms.exception.LoginException;
import cn.wolfcode.wms.mapper.BrandMapper;
import cn.wolfcode.wms.mapper.ProductMapper;
import cn.wolfcode.wms.page.PageResult;
import cn.wolfcode.wms.qo.ProductQueryObject;
import cn.wolfcode.wms.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper ProductMapper;
    @Autowired
    private BrandMapper brandMapper;
    public void deleteById(Long id) {
        ProductMapper.deleteByPrimaryKey(id);
    }

    public void updateOrSave(Product Product, MultipartFile img) {
        //查询到响应的品牌并注入
        Brand brand = brandMapper.selectByPrimaryKey(Product.getBrandId());
        Product.setBrandName(brand.getName());

        if (Product.getId() != null) {
            ProductMapper.updateByPrimaryKey(Product);
        } else {
            ProductMapper.insert(Product);
        }
    }

    public Product getById(Long id) {
        return ProductMapper.selectByPrimaryKey(id);
    }

    public List<Product> selectAll() {
        return ProductMapper.selectAll();
    }

    public PageResult query(ProductQueryObject qo) {
        Integer totalCount = ProductMapper.queryForCount(qo);
        if (totalCount == 0) {
            return new PageResult(qo.getPageSize());
        }
        List<Product> data = ProductMapper.queryForList(qo);
        return new PageResult(totalCount,data, qo.getCurrentPage(), qo.getPageSize());
    }

    public void deleteAll(Long[] ids) {
        if (ids != null) {
            for (Long id : ids) {
                ProductMapper.deleteByPrimaryKey(id);
            }
        }
    }
}
